﻿harem_politics_favored_consort_trigger = {
	OR = {
		has_character_modifier = favored_consort_modifier
		has_character_modifier = beloved_consort_modifier
		has_character_modifier = favor_boost_modifier
	}
	trigger_if = { #Can't be unfavored
		limit = { 
			has_variable = favor
		}
		trigger_if = { #Favor boost applies since neutral level
			limit = { 
				has_character_modifier = favor_boost_modifier
			}
			var:favor >= neutral_consort_lower_limit
		}
		trigger_else = {
			var:favor >= favorite_consort_lower_limit
		}
	}
	trigger_else = {
		NOT = {
			has_variable = favor
		}
	}
}

harem_politics_beloved_consort_trigger = {
	has_character_modifier = beloved_consort_modifier
	trigger_if = { #Can't be favored-
		limit = { 
			has_variable = favor
		}
		var:favor >= beloved_consort_lower_limit
	}
	trigger_else = {
		NOT = {
			has_variable = favor
		}
	}
}

harem_politics_unfavored_consort_trigger = {
	OR = {
		has_character_modifier = unfavored_consort_modifier
		has_character_modifier = forgotten_consort_modifier
	}
	NOT = {
		has_character_modifier = favor_boost_modifier
	}
	trigger_if = { #Can't be favored
		limit = { 
			has_variable = favor
		}
		var:favor < neutral_consort_lower_limit
	}
	trigger_else = {
		NOT = {
			has_variable = favor
		}
	}
}

harem_politics_forgotten_consort_trigger = {
	has_character_modifier = forgotten_consort_modifier
	NOT = {
		has_character_modifier = favor_boost_modifier
	}
	trigger_if = { #Can't be unfavored+
		limit = { 
			has_variable = favor
		}
		var:favor < unfavored_consort_lower_limit
	}
	trigger_else = {
		NOT = {
			has_variable = favor
		}
	}
}

harem_politics_punished_consort_trigger = {
	OR = {
		has_character_flag = flag_scolded_consort
		has_character_modifier = sleep_deprived_modifier
		has_character_modifier = intrigue_starved_and_deprived_modifier
		has_character_modifier = recently_tortured
		has_character_modifier = hunt_tasted_the_belt_modifier
	}
}

hp_accepts_harems = {
	OR = {
		hp_accepts_polygamy = yes
		hp_accepts_concubinage = yes
	}
}

#Favor system#
is_valid_for_favor_system_trigger = {
	is_courtier_of = $HAREM_LIEGE$
	is_alive = yes
	is_incapable = no
	OR = {
		is_imprisoned_by = $HAREM_LIEGE$
		is_imprisoned = no
	}
}


harem_politics_is_valid_authority_trigger = {
    # $AUTHORITY$ is the high-ranking character
    # $TARGET$ is the lower-ranked character
    # $LIEGE$ is the harem owner

    $AUTHORITY$ = { 
		is_valid_harem_member_trigger = {
			LIEGE = $LIEGE$
		}
	}
    $TARGET$ = {
		is_valid_harem_member_trigger = {
			LIEGE = $LIEGE$
		}
	}

    trigger_if = {
        # 1. If authority is a relative (not a consort), target must be a consort
		# 2. If authority is primary spouse, target must be a consort
        limit = {
            $AUTHORITY$ = {
				OR = {
					NOT = {
						is_consort_of = $LIEGE$
					}
					$LIEGE$.primary_spouse ?= { this = $AUTHORITY$ }
				}
            }
        }
    	$TARGET$ = {
			is_consort_of = $LIEGE$
		}
    }
    trigger_else_if = {
        # 3. If authority is Grand Consort, target must be a consort but not the PS
        limit = {
            $AUTHORITY$ = { has_court_position = grand_consort_court_position }
        }
		$TARGET$ = {
			is_consort_of = $LIEGE$
			trigger_if = {
				limit = {
					exists = $LIEGE$.primary_spouse
				}
				NOT = {
					this = $LIEGE$.primary_spouse
				}
			}
		}
    }
    trigger_else_if = {
        # 4. If authority is a spouse (not PS), target must be a concubine
        limit = {
            $AUTHORITY$ = {
                is_spouse_of = $LIEGE$
            }
        }
        $TARGET$ = { is_concubine_of = $LIEGE$ }
    }
    trigger_else = {
        always = no
    }
}

##Potential consorts##
bride_show_viable_consort_trigger = {
	is_healthy = yes
	is_available = yes
	age <= 23
	NOR = {
		#If you betrothed them already, why should they participate?
		betrothed ?= { is_close_family_of = $OVERLORD$ }
		has_council_position = councillor_court_chaplain
		is_close_family_of = $OVERLORD$
		has_trait = devoted
		has_trait = celibate
		has_trait = lunatic_1
		has_trait = lunatic_genetic
		has_trait = possessed_1
		has_trait = possessed_genetic
		has_trait = lisping
		has_trait = stuttering
		has_trait = scaly
		has_trait = clubfooted
		has_trait = hunchbacked
		has_trait = wheezing
		has_trait = spindly
		has_trait = one_eyed
		has_trait = one_legged
		has_trait = disfigured
		has_trait = eunuch
		has_trait = adulterer
		has_trait = fornicator
		has_trait = deviant
		has_trait = murderer
		has_trait = kinslayer_1
		has_trait = kinslayer_2
		has_trait = kinslayer_3
		has_trait = incestuous
		has_trait = excommunicated
		has_trait = drunkard
		has_trait = hashishiyah
		has_trait = wounded
		has_trait = sickly
		has_trait = infirm
		has_trait = incapable
		has_trait = inbred
		has_trait = maimed
		has_trait = blind
		has_trait = ill
		has_trait = bubonic_plague
		has_trait = cancer
		has_trait = consumption
		has_trait = great_pox
		has_trait = gout_ridden
		has_trait = leper
		has_trait = lovers_pox
		has_trait = pneumonic
		has_trait = smallpox
		has_trait = typhus
		has_trait = dwarf
		has_trait = physique_bad_3	#Weak candidates wouln't make it to the capital without getting sick.
		
		has_trait = dysentery
		has_trait = ergotism
		has_trait = measles

		has_trait = pregnant
	}
	can_become_concubine_of_character_valid_trigger = { CHARACTER = $OVERLORD$ }
	NOT = {
		any_heir_title = {
			place_in_line_of_succession = {
				target = prev
				value <= 2
			}
		}
	}
}

bride_show_viable_candidate_trigger = {
	bride_show_viable_consort_trigger = {
		OVERLORD = $OVERLORD$
	}
	trigger_if = { #If they aren't betrothed, you can arrange a marriage with them
		limit = {
			age = 15
		}
		betrothed ?= {
			NOT = {
				is_close_family_of = $OVERLORD$
			}
		}
		$OVERLORD$ = {
			is_ai = no
		}
	}
	trigger_else = {
		age >= 16
	}
	trigger_if = { #Beauty parameters
		limit = {
			OR = { #Tributaries would never send ugly candidates
				$OVERLORD$ = { has_variable = beauty_important }
				court_owner ?= {
					is_tributary_of = $OVERLORD$
				}
			}
		}
		NOR = {
			has_character_modifier = malnourished_modifier
			current_weight < -50
			has_character_modifier = obese_modifier
			current_weight > 50
			has_trait = beauty_bad
		}
	}
	trigger_if = { #Intellectual parameters
		limit = {
			OR = { #Tributaries would never send stupid candidates
				$OVERLORD$ = { has_variable = personality_skills_important }
				court_owner ?= {
					is_tributary_of = $OVERLORD$
				}
			}
		}
		NOT = {
			has_trait = intellect_bad
		}
	}
}

recruit_concubines_viable_candidate_trigger = {
	is_adult = yes
	is_councillor = no
	bride_show_viable_consort_trigger = {
		OVERLORD = $OVERLORD$
	}
	NOR = {
		religion = religion:islam_religion
		has_trait = beauty_bad
		has_character_flag = recently_abducted_concubine
		any_close_family_member = {
			highest_held_title_tier >= tier_county
		}
		any_consort = {
			count >= 1
		}
	}
	trigger_if = {
		limit = {
			exists = court_owner
		}
		court_owner = {
			NOR = {
				is_liege_or_above_of = $OVERLORD$
				is_vassal_or_below_of = $OVERLORD$
				is_tributary_of = $OVERLORD$
			}
		}
	}
}

recruit_concubines_viable_county_trigger = {
	OR = {
		any_pool_character = {
			province = this
			recruit_concubines_viable_candidate_trigger = {
				OVERLORD = $OVERLORD$
			}
		}
		NOT = {
			religion = religion:islam_religion
		}
	}
}

#Bride Show subject requirements#
harem_politics_valid_bride_show_subject_trigger = {
	OR = {
		is_vassal_of = $OVERLORD$
		is_tributary_of = $OVERLORD$
	}
	NOR = {	
		is_at_war_with = $OVERLORD$
		government_has_flag = government_is_theocracy
		government_has_flag = government_is_republic
		government_has_flag = government_is_herder
		has_opinion_modifier = {
			modifier = demanded_concubine_opinion
			target = $OVERLORD$
		}
		has_character_modifier = isolating_modifier
		capital_county = { has_county_modifier = isolate_capital_decision_modifier }
	}
	is_imprisoned = no
	trigger_if = {
		limit = {
			$OVERLORD$ = {
				government_allows = administrative
			}
			government_allows = administrative
			is_vassal_of = $OVERLORD$
		}
		NOT = {
			house ?= {
				any_house_member = {
					is_liege_or_above_of = $OVERLORD$
				}
			}
		}
	}
	trigger_else = {
		any_close_family_member = {
			NOR = {
				is_liege_or_above_of = $OVERLORD$
				any_tributary = {
					this = $OVERLORD$
				}
			}
		}
	}
}
harem_politics_minimum_valid_subject_requirements_trigger = {
	harem_politics_valid_bride_show_subject_trigger = {
		OVERLORD = $OVERLORD$
	}
	trigger_if = { #For a nomad's subjects, what matters it their obedience.
		limit = {
			$OVERLORD$ = {	
				government_has_flag = government_is_nomadic
			}
		}
		is_obedient_to = $OVERLORD$
	}
	trigger_else = { #Non-nomadic requirements
		trigger_if = { #Tributary requirements
			limit = {
				NOT = {
					is_vassal_of = $OVERLORD$
				}
			}
			trigger_if = { #Else, if they rank the same as their suzerain, they need to dread them
				limit = {
					highest_held_title_tier = $OVERLORD$.highest_held_title_tier
				}
				OR = {
					has_dread_level_towards = {
						target = $OVERLORD$
						level >= 1
					}
					opinion = {
						target = $OVERLORD$
						value >= 15
					}
				}
			}
			trigger_else_if = { #Else, they need to dread them more
				limit = {
					highest_held_title_tier > $OVERLORD$.highest_held_title_tier
				}
				OR = {
					has_dread_level_towards = {
						target = $OVERLORD$
						level >= 2
					}
					opinion = {
						target = $OVERLORD$
						value >= 30
					}
				}
			}
			trigger_else = { #Or lower tier than their suzerain
				highest_held_title_tier < $OVERLORD$.highest_held_title_tier
				opinion = {
					target = $OVERLORD$
					value >= -10
				}
			}
		}
		trigger_else = { #Vassals are always valid
			always = yes
		}
	}
}
harem_politics_is_valid_bride_show_subject_trigger = {
    harem_politics_valid_bride_show_subject_trigger = { OVERLORD = $OVERLORD$ }
	trigger_if = {
		limit = {
			$OVERLORD$ = {
				government_has_flag = government_is_nomadic
			}
		}
		trigger_if = {
			limit = {
				is_vassal_of = $OVERLORD$
			}
			is_obedient_to = $OVERLORD$
		}
		trigger_else = {
			# Tributaries are always valid for nomadic rulers
			always = yes
		}
	}
    trigger_else_if = { # Dukes
        limit = { $OVERLORD$ = { highest_held_title_tier = tier_duchy } }
        trigger_if = { # Only sovereigns can host a bride show
            limit = { 
				$OVERLORD$ = { 
					government_allows = administrative
				}
			}
            $OVERLORD$ = {
				is_governor = no
				is_independent_ruler = yes
			}
        }
        trigger_else_if = { # Feudal/Clan/Tribal Duke
            limit = { 
				$OVERLORD$ = { 
					NOR = { 
						government_allows = administrative 
						government_has_flag = government_is_nomadic 
					} 
				} 
			}
            trigger_if = { # Low Authority
                limit = { 
					$OVERLORD$ = { 
						OR = { 
							has_realm_law = crown_authority_0
							has_realm_law = tribal_authority_0 
						}
					} 
				}
				trigger_if = {
					limit = {
						is_vassal_of = $OVERLORD$
					}
					is_powerful_vassal = no
				}
				trigger_else = { #Nothing else to check
					always = yes
				}
            }
			trigger_else_if = { #Limited Authority+
				limit = {
					$OVERLORD$ = {
						NOR = {
							has_realm_law = crown_authority_0
							has_realm_law = tribal_authority_0
						}
					}
				}
				#Nothing else to check
				always = yes
			}
			trigger_else = { #Nothing else to check
				always = yes
			}
        }
		trigger_else = { #Nothing else to check
			always = no
		}
    }
    trigger_else_if = { # Kings
        limit = { $OVERLORD$ = { highest_held_title_tier = tier_kingdom } }
        trigger_if = { # Administrative King
            limit = { 
				$OVERLORD$ = { 
					government_allows = administrative 
				} 
			}
			$OVERLORD$ = {
				is_independent_ruler = yes
			}
            trigger_if = { # Low Imperial Bureaucracy
                limit = { 
					$OVERLORD$ = { 
						has_realm_law = imperial_bureaucracy_0 
					} 
				}
				trigger_if = {
					limit = {
						is_vassal_of = $OVERLORD$
					}
					trigger_if = {
						limit = { 
							government_allows = administrative 
						}
						house ?= { 
							is_dominant_family = no 
							is_powerful_family = no
						}
					}
					trigger_else = {
						is_powerful_vassal = no
						highest_held_title_tier <= tier_county
					}
				}
				trigger_else = { #Nothing else to check
					always = yes
				}
            }
            trigger_else_if = { # Limited Imperial Bureaucracy
                limit = { 
					$OVERLORD$ = { 
						has_realm_law = imperial_bureaucracy_1 
					} 
				}
				trigger_if = {
					limit = {
						is_vassal_of = $OVERLORD$
					}
					trigger_if = {
						limit = { government_allows = administrative }
						house ?= { 
							is_dominant_family = no 
						}
					}
					trigger_else = { is_powerful_vassal = no }
				}
				trigger_else = { #Nothing else to check
					always = yes
				}
            }
            trigger_else = { # High Imperial Bureaucracy+
                #Nothing else to check
				always = yes
            }
        }
        trigger_else_if = { # Feudal/Clan/Tribal King
            limit = { 
				$OVERLORD$ = {
					NOR = {
						government_allows = administrative 
						government_has_flag = government_is_nomadic 
					}
				} 
			}
            trigger_if = { # Low Authority
                limit = { 
					$OVERLORD$ = { 
						OR = {
							has_realm_law = crown_authority_0
							has_realm_law = tribal_authority_0
						}
					} 
				}
				trigger_if = {
					limit = {
						is_vassal_of = $OVERLORD$
					}
					is_powerful_vassal = no
					highest_held_title_tier <= tier_county
				}
				trigger_else = { #Nothing else to check
					always = yes
				}
            }
            trigger_else_if = { # Limited Authority
                limit = { 
					$OVERLORD$ = { 
						OR = {
							has_realm_law = crown_authority_1
							has_realm_law = tribal_authority_1
						}
					} 
				}
				trigger_if = {
					limit = {
						is_vassal_of = $OVERLORD$
					}
					is_powerful_vassal = no
				}
				trigger_else = { #Nothing else to check
					always = yes
				}
            }
            trigger_else = { always = yes }
        }
		trigger_else = { #Nothing else to check
			always = no
		}
    }
    trigger_else_if = { # Emperors
        limit = { 
			$OVERLORD$ = { 
				highest_held_title_tier = tier_empire 
			} 
		}
        trigger_if = { # Administrative Emperor
            limit = { 
				$OVERLORD$ = { 
					government_allows = administrative 
				} 
			}
            trigger_if = { # Low Imperial Bureaucracy
                limit = { 
					$OVERLORD$ = { 
						has_realm_law = imperial_bureaucracy_0 
					} 
				}
				trigger_if = {
					limit = {
						is_vassal_of = $OVERLORD$
					}
					trigger_if = {
						limit = {
							government_allows = administrative
						}
						house ?= { 
							is_dominant_family = no 
							is_powerful_family = no
						}
					}
					trigger_else = {
						is_powerful_vassal = no
						highest_held_title_tier <= tier_duchy
					}
				}
				trigger_else = { #Nothing else to check
					always = yes
				}
            }
            trigger_else_if = { # Limited Imperial Bureaucracy
                limit = { 
					$OVERLORD$ = { 
						has_realm_law = imperial_bureaucracy_1 
					} 
				}
				trigger_if = {
					limit = {
						is_vassal_of = $OVERLORD$
					}
					trigger_if = {
						limit = {
							government_allows = administrative
						}
						house ?= { 
							is_dominant_family = no
						}
					}
					trigger_else = {
						is_powerful_vassal = no
					}
				}
				trigger_else = { #Nothing else to check
					always = yes
				}
            }
            trigger_else = { # High Imperial Bureaucracy+
                always = yes
            }
        }
        trigger_else_if = { # Feudal/Clan/Tribal Emperor
            limit = { 
				$OVERLORD$ = { 
					NOR = {
						government_allows = administrative
						government_has_flag = government_is_nomadic
					}
				} 
			}
            trigger_if = { # Low Authority
                limit = { 
					$OVERLORD$ = { 
						OR = {
							has_realm_law = crown_authority_0
							has_realm_law = tribal_authority_0
						}
					} 
				}
				trigger_if = {
					limit = {
						is_vassal_of = $OVERLORD$
					}
					is_powerful_vassal = no
					highest_held_title_tier <= tier_duchy
				}
				trigger_else = { #Nothing else to check
					always = yes
				}
            }
            trigger_else_if = { # Limited Authority
                limit = { 
					$OVERLORD$ = { 
						OR = {
							has_realm_law = crown_authority_1
							has_realm_law = tribal_authority_1
						}
					} 
				}
				trigger_if = {
					limit = {
						is_vassal_of = $OVERLORD$
					}
					is_powerful_vassal = no
				}
				trigger_else = { #Nothing else to check
					always = yes
				}
            }
            trigger_else = { always = yes }
        }
		trigger_else = { #Nothing else to check
			always = no
		}
    }
	trigger_else = { #Nothing else to check
		always = no
	}
}

#Grand Consort Tasks
harem_politics_grand_consort_task_valid_neighboring_ruler_trigger = {
	NOT = {
		is_tributary_of = $OVERLORD$
	}
	harem_politics_grand_consort_task_valid_basic_ruler_trigger = {
		OVERLORD = $OVERLORD$
	}
}

harem_politics_grand_consort_task_valid_vassal_trigger = {
	highest_held_title_tier >= tier_county
	harem_politics_grand_consort_task_valid_basic_ruler_trigger = {
		OVERLORD = $OVERLORD$
	}
}

harem_politics_grand_consort_task_valid_tributary_trigger = {
	trigger_if = {
		limit = {
			$OVERLORD$ = {
				government_has_flag = government_is_nomadic
			}
		}
		NOT = { is_obedient_to = $OVERLORD$ }
	}
	trigger_else = {
		trigger_if = { # Same rank tributary needs dread level 1
			limit = {
				highest_held_title_tier = $OVERLORD$.highest_held_title_tier
			}
			has_dread_level_towards = {
				target = $OVERLORD$
				level < 1
			}
		}
		trigger_else_if = { # Higher rank tributary needs dread level 2
			limit = {
				highest_held_title_tier > $OVERLORD$.highest_held_title_tier
			}
			has_dread_level_towards = {
				target = $OVERLORD$
				level < 2
			}
		}
		trigger_else = { # Low opinion check
			opinion = {
				target = $OVERLORD$
				value <= -10
			}
		}
	}
	harem_politics_grand_consort_task_valid_basic_ruler_trigger = {
		OVERLORD = $OVERLORD$
	}
}

harem_politics_grand_consort_task_valid_basic_ruler_trigger = {
	NOR = {
		has_opinion_modifier = {
			modifier = impressed_opinion
			target = $OVERLORD$
		}
		is_at_war_with = $OVERLORD$
		government_has_flag = government_is_herder
	}
	trigger_if = {
		limit = {
			NOT = {
				is_tributary_of = $OVERLORD$
			}
		}
		opinion = {
			target = $OVERLORD$
			value <= 5
		}
	}
}

# Valid neighboring counties#
harem_politics_valid_event_county_trigger = {
	trigger_if = {
		limit = {
			religion = religion:islam_religion
		}
		OR = {
			any_neighboring_and_across_water_top_liege_realm_owner = {
				OR = {
					any_realm_province = {
						NOT = {
							religion = religion:islam_religion
						}
					}
					any_tributary = {
						any_realm_province = {
							NOT = {
								religion = religion:islam_religion
							}
						}
					}
				}
			}
			any_realm_province = {
				NOT = {
					religion = religion:islam_religion
				}
			}
		}
	}	
	trigger_else = {
		NOT = {
			religion = religion:islam_religion
		}
	}
}